package com.cci.kangdao.controller;

import com.cci.kangdao.service.MobileSmsService;
import com.cci.kangdao.utilTool.ParameterTool;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;
/**
 * @Description: 发送手机验证码及验证
 * @Company: CTY
 * @author 米艳春
 * @date 2018-01-17
 * @version 1.0
 */
@Controller
public class MobileSms {
	private Logger log = Logger.getLogger(this.getClass().getName());

	@Autowired
	private MobileSmsService mobileSmsService;

	/**
	 * App获取短信接口
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/getMobileSms.aspx")
	public ModelAndView getMobileSms(HttpServletRequest request,
			HttpServletResponse response){
		
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			return mobileSmsService.getMobileSms(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}

	/**
	 * App验证手机号接口
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/verifyMobile.aspx")
	public ModelAndView verifyMobile(HttpServletRequest request,
			HttpServletResponse response){
		
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			return mobileSmsService.verifyMobile(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}
	
	/**
	 * 通过邮箱获取验证码
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/getCodeByEmail.aspx")
	public ModelAndView getCodeByEmail(HttpServletRequest request,
			HttpServletResponse response){
		
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			HttpSession session = request.getSession(false);
			if (session != null) {
				session.invalidate();
			}
			request.getSession(true);
			return mobileSmsService.getCodeByEmail(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}
	
	/**
	 * 验证邮箱验证码
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/verifyEmailCode.aspx")
	public ModelAndView verifyEmailCode(HttpServletRequest request,
			HttpServletResponse response){
		
		try{
			Map<String, Object> map = ParameterTool.getParameterRequest(request, true);
			map.put("response", response);
			return mobileSmsService.verifyEmailCode(map);
		}catch(Exception ex){
			log.error(ex.getMessage(),ex);	
			
			String msg = "{\"status\": -99,\"msg\": \"未知错误\"}";
			ParameterTool.writeResponse(response, msg);	
		}
		return null;
	}
}
